import os
import sys
import getopt

def main():
    src_dir = ''
    des_dir = ''
    s_code = 'utf-8'
    d_code = 'gbk'
    opts, args = getopt.getopt(sys.argv[1:], 'h', ['src_dir=', 'des_dir=', 's_code=', 'd_code='])
    for o, a in opts:
        if o == '-h':
            print('change_code.py --src_dir=./source_dir --des_dir=./des_dir --s_code=utf-8 --d_code=gbk')
        if o == '--src_dir':
            src_dir = a
        if o == '--des_dir':
            des_dir = a
        if o == '--s_code':
            s_code = a
        if o == '--d_code':
            d_code = a

    print('源目录：', src_dir, '\n生成文件目录:', des_dir, '\n从', s_code, '转为', d_code)
    if not os.path.exists(des_dir):
        os.makedirs(des_dir)

    file_list = os.listdir(src_dir)
    for f in file_list:
        src_path = src_dir + '/' + f
        with open(src_path, encoding=s_code) as in_f:
            des_path = des_dir + '/' + f
            print('save', des_path, end='\t')
            with open(des_path, mode='w', encoding=d_code) as out_f:
                for line in in_f:
                    out_f.write(line)
            print('done')


if __name__ == '__main__':
    main()
